package bar.dark.lee.setting.sms;

import java.util.ArrayList;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;
import bar.dark.lee.R;
import bar.dark.lee.SuperActivity;
import bar.dark.lee.common.BDUtil;
import bar.dark.lee.common.DBSQLite;
import bar.dark.lee.common.DBSQLite.ParseSelectData;
import bar.dark.lee.setting.TipInfoActivity;
import bar.dark.lee.setting.TipsActivity;

public class SMSActivity extends SuperActivity implements OnClickListener{
	
	private ListView mListView;
	private ArrayList<PaySMSData> mArrayPaySMSData;
	private ArrayList<String> mArrayAddress;
	private AlertDialog dialog;
	private DBSQLite sql;
	private View dialogView;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) 
	{
		isAdFlag = false;
		
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.sms_activity);

		sql = DBSQLite.getInstance(this);
		
		initFindView();
		initData();
		
		setTitle("문자 자동화");
		
//		Cursor c = getContentResolver().query(Uri.parse("content://sms/inbox/"), null, null, null, null);
//		if( c.moveToFirst() ) {
//			String address = "";
//			String body = "";
//			while( c.moveToNext() ) {
//				address = c.getString(c.getColumnIndex("address"));
//				body = c.getString(c.getColumnIndex("body"));
//				
//				SMSData data = new SMSData();
//				data.setAddress(address);
//				data.setBody(body);
//				mArraySMSData.add(data);
//			}
//		}
//
//		mAdapterSMSData.notifyDataSetChanged();
		
//		PaySMSData paySMSData = new PaySMSData();
//		paySMSData.setAddress("15881000");
//		paySMSData.setName("농협체크카드");
//		mArrayPaySMSData.add(paySMSData);
//		paySMSData = new PaySMSData();
//		paySMSData.setAddress("15448888");
//		paySMSData.setName("외환카드");
//		mArrayPaySMSData.add(paySMSData);
//		paySMSData = new PaySMSData();
//		paySMSData.setAddress("15114433");
//		paySMSData.setName("현대카드M");
//		mArrayPaySMSData.add(paySMSData);
//		paySMSData = new PaySMSData();
//		paySMSData.setAddress("12341234");
//		paySMSData.setName("신한카드");
//		mArrayPaySMSData.add(paySMSData);
//		paySMSData = new PaySMSData();
//		paySMSData.setAddress("15553423");
//		paySMSData.setName("올앳카드");
//		mArrayPaySMSData.add(paySMSData);
//		
//		mAdapterPaySMSData.notifyDataSetChanged();
		
//		InputData data = new InputData();
////		String test = "KB국민체크(6*0*)\n이효준님\n09/03 13:25\n11,600원\n(주)훼미리마트 사용";	// 돈뒤
////		String test = "[외환카드]이효준님     5,000원 승인 AS헤어 10/03 12:12";	// 돈뒤
////		String test = "[현대카드M]-승인 이효준님 03/23 20:18 4,980원(일시불) (주)왕대박";	// 돈뒤
////		String test = "신한카드정상승인이효준님 04/21 09:02 30,000원(일시불) (주) 인터파크";	// 돈뒤
//		String test = "신한카드정상승인이효준님 04월21일 09:02 30,000원(일시불) (주) 인터파크";	// 돈뒤
////		String test = "[올앳카드]이효준님 승인내역 09/03 11:11 통일석유(주) 100,000,000원 잔액500원";	// 시간뒤
////		String test = "[체크.승인]32,550원 우리BC(8*5*) 이효준님 09/03 15:15 (주)이니스프리";	// 맨뒤
////		String test = "[현대카드M]-승인\n이진수님\n08/14 04:59\n50,000원(일시불)\n해물나라짬뽕마을";	// 돈뒤
//		SMSParser parse = new SMSParser(test);
//		data = parse.messageParsing();
//		if (data != null) {
//			Log.e("1", "name : "+data.getName());
//			Log.e("1", "date : "+data.getDate());
//			Log.e("1", "money : "+data.getMoney());
//			Log.e("1", "memo1 : "+data.getMemo1());	// 돈뒤
//			Log.e("1", "memo2 : "+data.getMemo2());	// 시간뒤
//			Log.e("1", "memo3 : "+data.getMemo3());	// 맨뒤
//		}
	}
	
	private void initFindView() {
		mListView = (ListView) findViewById(R.id.sms_listview);
		
		mArrayPaySMSData = new ArrayList<PaySMSData>();
		mArrayAddress = new ArrayList<String>();
		//mAdapterPaySMSData = new PaySMSAdapter(SMSActivity.this, R.layout.list_item_sms, mArrayPaySMSData);
		//mAdapterPaySMSData.setNotifyOnChange(true);
		//mListView.setAdapter(mAdapterPaySMSData);
		//mListView.setOnItemClickListener(mAdapterPaySMSData);
		
		findViewById(R.id.sms_btnAdd).setOnClickListener(this);
		findViewById(R.id.sms_btnSave).setOnClickListener(this);
		
		LayoutInflater li = (LayoutInflater) getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
		resultView = li.inflate(R.layout.view_no_result, null);
	}
	
	
	int radioCheck;
	
	private void initData() {
		
		SharedPreferences pref = getSharedPreferences("pref", Activity.MODE_PRIVATE);
		radioCheck = pref.getInt("smsRadioGroup", 1);
		
		getPaySMSData();
		//mAdapterPaySMSData.notifyDataSetChanged();
	}
	
	@Override
	protected void onResume() {
		super.onResume();
		new ShowDialog().execute(BDUtil.asyncFlag, 1);
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		
	}
	
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
			case R.id.sms_btnSave:
				
				new ShowDialog().execute(BDUtil.asyncFlag, 2);
				
			break;
			case R.id.sms_btnAdd:
				
				new ShowDialog().execute(BDUtil.asyncFlag, 3);
				
			break;
	
			default:
			break;
		}
	}
	
	private void noSMS() {
		// 다 실패하면 그냥 직접입력해한다
		
		if (dialog != null) {
			dialog.dismiss();
			dialog = null;
		}
		
		dialogView = LayoutInflater.from(this).inflate(R.layout.sms_dialog3, null);
//		ListView listView = (ListView) view.findViewById(R.id.smsDialog_listView);
//		ArrayList<SMSData> list = new ArrayList<SMSData>();
//		SMSAdapter adapter = new SMSAdapter(this, R.layout.list_item_sms2, list); 
//		listView.setAdapter(adapter);
//		listView.setOnItemClickListener(adapter);
//		Cursor c = getContentResolver().query(Uri.parse("content://sms/inbox/"), null, null, null, null);
//		if( c.moveToFirst() ) {
//			String address = "";
//			String body = "";
//			String date = "";
//			while( c.moveToNext() ) {
//				date = c.getString(c.getColumnIndex("date"));
//				address = c.getString(c.getColumnIndex("address"));
//				body = c.getString(c.getColumnIndex("body"));
//				
//				Date date2 = new Date(Long.parseLong(date));
//				SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
//				date = sdf.format(date2);
//				SMSData data = new SMSData();
//				data.setAddress(address);
//				data.setBody(body);
//				data.setDate(date);
//				list.add(data);
//			}
//		}
//		adapter.setNotifyOnChange(true);
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setView(dialogView);
		
//		dialogView.findViewById(R.id.smsDialog3_btnInbox).setOnClickListener(new OnClickListener() {
//			@Override
//			public void onClick(View v) {
//				Intent intent = new Intent(Intent.ACTION_VIEW);
//				intent.putExtra("sms_body", "The SMS text");
//				intent.setType("vnd.android-dir/mms-sms");
//				startActivity(intent);
//			}
//		});
		dialogView.findViewById(R.id.smsDialog3_btnConfirm).setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				String inputNumber = ((EditText)dialogView.findViewById(R.id.smsDialog3_editNumber)).getText().toString().trim();
				String inputMessage = ((EditText)dialogView.findViewById(R.id.smsDialog3_editMessage)).getText().toString().trim();
				
				if (inputNumber.length()==0) {
//					BDUtil.showDialog(SMSActivity.this, "번호누락", "문자 번호를 입력해주세요.");
					BDUtil.showToast(SMSActivity.this, "문자 번호를 입력해주세요.");
					return;
				}
				
				try {
					Integer.parseInt(inputNumber);
				} catch (Exception e) {
					BDUtil.showToast(SMSActivity.this, "올바른 문자 번호를 입력해주세요.");
					return;
				}
				
				if (inputMessage.length()==0) {
//					BDUtil.showDialog(SMSActivity.this, "내용누락", "문자 내용을 입력해주세요.");
					BDUtil.showToast(SMSActivity.this, "문자 내용을 입력해주세요.");
					return;
				}
				
				//SMSParser2 parser = new SMSParser2(inputMessage);
				Parser parser = null;
				try {
					parser = new Parser(inputMessage);
				} catch (Exception e)
				{
					Toast.makeText(superContext, "문자 분석중에 문제가 생겼습니다. 개발자에게 메일을 보내주세요.", Toast.LENGTH_SHORT).show();
					return;
				} finally
				{
					if (parser == null)
					{
						Toast.makeText(superContext, "문자 분석중에 문제가 생겼습니다. 개발자에게 메일을 보내주세요.", Toast.LENGTH_SHORT).show();
						return;
					}
				}
				InputData data = parser.messageParsing();
				if (data == null || !parser.getAvailable()) 
				{
					BDUtil.showToast(SMSActivity.this, "지원하지 않는 문자 형식입니다..");
					return;
//					SMSParser parser2 = new SMSParser(inputMessage);
//					data = new InputData();
//					data = parser2.messageParsing();
//					if (data == null || !parser2.getAvailable())
//					{
////						BDUtil.showDialog(SMSActivity.this, "연동불가문자", "지원하지 않는 문자 형식입니다.");
//						BDUtil.showToast(SMSActivity.this, "지원하지 않는 문자 형식입니다..");
//						return;
//					}
				}
				
				for (int i=0; i<mArrayPaySMSData.size(); i++) {
					if (mArrayPaySMSData.get(i).getName().equalsIgnoreCase(data.getName())) {
						BDUtil.showDialog(SMSActivity.this, "중복등록", "이미 문자 자동화가 등록된 카드입니다.");
						return;
					}
				}
				
//				for (int i=0; i<mArrayPaySMSData.size(); i++) {
//					if (inputNumber.equalsIgnoreCase(mArrayPaySMSData.get(i).getAddress())) {
////						BDUtil.showDialog(SMSActivity.this, "연동불가문자", "동일한 번호의 결제문자가 이미 등록되어 있습니다.");
//						BDUtil.showToast(SMSActivity.this, "동일한 번호의 연동문자가 이미 등록되어 있습니다.");
//						return;
//					}
//				}
				
				Intent intent = new Intent(SMSActivity.this, SMSSettingActivity.class);
				intent.putExtra("address", inputNumber);
				intent.putExtra("body", inputMessage);
				intent.putExtra("where", "SMSActivity");
				startActivity(intent);
				if (dialog != null) {
					dialog.dismiss();
					dialog = null;
				}
			}
		});
		dialogView.findViewById(R.id.smsDialog3_btnCancel).setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if (dialog != null) {
					dialog.dismiss();
					dialog = null;
				}
			}
		});
		
		dialog = builder.create();
		dialog.show();
//		builder.setPositiveButton("확인", new DialogInterface.OnClickListener() {
//			@Override
//			public void onClick(DialogInterface dialog, int which) {
//				
//				String inputNumber = ((EditText)dialogView.findViewById(R.id.smsDialog3_editNumber)).getText().toString();
//				String inputMessage = ((EditText)dialogView.findViewById(R.id.smsDialog3_editMessage)).getText().toString();
//				
//				if (inputNumber.length()==0) {
////					BDUtil.showDialog(SMSActivity.this, "번호누락", "문자 번호를 입력해주세요.");
//					BDUtil.showToast(SMSActivity.this, "문자 번호를 입력해주세요.");
//					return;
//				}
//				
//				if (inputMessage.length()==0) {
////					BDUtil.showDialog(SMSActivity.this, "내용누락", "문자 내용을 입력해주세요.");
//					BDUtil.showToast(SMSActivity.this, "문자 내용을 입력해주세요.");
//					return;
//				}
//				
//				SMSParser parser = new SMSParser(inputMessage);
//				parser.messageParsing();
//				if (!parser.getAvailable()) {
////					BDUtil.showDialog(SMSActivity.this, "연동불가문자", "지원하지 않는 문자 형식입니다.");
//					BDUtil.showToast(SMSActivity.this, "지원하지 않는 문자 형식입니다..");
//					return;
//				}
//				
//				for (int i=0; i<mArrayPaySMSData.size(); i++) {
//					if (inputNumber.equalsIgnoreCase(mArrayPaySMSData.get(i).getAddress())) {
////						BDUtil.showDialog(SMSActivity.this, "연동불가문자", "동일한 번호의 결제문자가 이미 등록되어 있습니다.");
//						BDUtil.showToast(SMSActivity.this, "동일한 번호의 결제문자가 이미 등록되어 있습니다.");
//						return;
//					}
//				}
//				
//				Intent intent = new Intent(SMSActivity.this, SMSSettingActivity.class);
//				intent.putExtra("address", inputNumber);
//				intent.putExtra("body", inputMessage);
//				startActivity(intent);
//				if (dialog != null) {
//					dialog.dismiss();
//					dialog = null;
//				}
//			}
//		});
//		builder.setNegativeButton("취소", new DialogInterface.OnClickListener() {
//			@Override
//			public void onClick(DialogInterface dialog, int which) {
//			}
//		});
//		dialog = builder.create();
//		dialog.show();
	}
	
	
	private void setAddListView(final ArrayList<SMSData> list) {
		
		View view = LayoutInflater.from(this).inflate(R.layout.sms_dialog, null);
		Button button = (Button) view.findViewById(R.id.smsDialog_direct);
		button.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				noSMS();
			}
		});
		ListView listView = (ListView) view.findViewById(R.id.smsDialog_listView);
		SMSAdapter adapter = new SMSAdapter(this, R.layout.list_item_sms2, list); 
		listView.setAdapter(adapter);
		listView.setOnItemClickListener(adapter);
		adapter.setNotifyOnChange(true);
		
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setView(view);
		
		dialog = builder.create();
		dialog.show();
	}
	
	/**
	 * 결제문자정보 데이터 가져온다
	 */
	private void getPaySMSData() {
		
		mArrayPaySMSData.clear();
		mArrayAddress.clear();
		
		String query = "";
		
		query = "SELECT id, address, name FROM sms"; 
		
		sql.selectMapTile(this, query, new ParseSelectData()
		{
			@Override
			public void resultData(Cursor c)
			{
				int count = c.getCount();
				c.moveToFirst();
				
				int idIndex = c.getColumnIndex("id");
				int addressIndex = c.getColumnIndex("address");
				int nameIndex = c.getColumnIndex("name");
				
				for (int i = 0; i < count; i++)
				{
					PaySMSData data = new PaySMSData();
					data.setId(c.getInt(idIndex) + "");
					data.setAddress(c.getString(addressIndex));
					data.setName(c.getString(nameIndex) + "");
					mArrayPaySMSData.add(data);
					mArrayAddress.add(c.getString(addressIndex));
					c.moveToNext();
				}
			}
		});
	}
	
	private class PaySMSData
	{
		String id;
		String address;
		String name;
		
		public String getId() {
			return id;
		}
		public void setId(String id) {
			this.id = id;
		}
		public String getAddress() {
			return address;
		}
		public void setAddress(String address) {
			this.address = address;
		}
		public String getName() {
			return name;
		}
		public void setName(String name) {
			this.name = name;
		}
	}
	
	private class PaySMSAdapter extends ArrayAdapter<PaySMSData> implements OnItemClickListener
	{
		Context context;
		int viewId;
		ArrayList<PaySMSData> items;
		
		public PaySMSAdapter(Context context, int textViewResourceId, ArrayList<PaySMSData> objects) {
			super(context, textViewResourceId, objects);
			this.context = context;
			this.viewId = textViewResourceId;
			this.items = objects;
		}
		
		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			View view = convertView;
			
			if (view == null) {
				view = LayoutInflater.from(context).inflate(viewId, null);
			}
			
			PaySMSData data = items.get(position);
			
			((TextView) view.findViewById(R.id.listSMSAddress)).setText(data.getAddress());
			((TextView) view.findViewById(R.id.listSMSName)).setText(data.getName());
			
			return view;
		}
		
		@Override
		public void onItemClick(AdapterView<?> arg0, View arg1, int postion,
				long arg3)
		{
			/*final PaySMSData data = items.get(postion);
			
			AlertDialog.Builder dia = new AlertDialog.Builder(SMSActivity.this);
			
			dia.setTitle("결제문자 삭제/수정");
			dia.setIcon(android.R.drawable.ic_dialog_info);
			
			dia.setPositiveButton("삭제", new DialogInterface.OnClickListener()
			{
				@Override
				public void onClick(DialogInterface dialog, int which)
				{
					setDeleteData(data);
				}
			});
			
			dia.setNeutralButton("수정", new DialogInterface.OnClickListener()
			{
				@Override
				public void onClick(DialogInterface dialog, int which)
				{
					setUpdateData(data);
				}
			});
			
			dia.show();*/
			
			PaySMSData data  = (PaySMSData) items.get(postion);
			
			Intent intent = new Intent(SMSActivity.this, SMSUpdateActivity.class);
			intent.putExtra("id", data.getId());
			intent.putExtra("address", data.getAddress());
			intent.putExtra("cardname", data.getName());
			intent.putStringArrayListExtra("addressList", mArrayAddress);
			startActivityForResult(intent, 0);
		}
	}

	@Override
	protected void tipButton()
	{
		Intent intent = new Intent(this, TipInfoActivity.class);
		intent.putExtra(TipsActivity.TIP_REQUEST_KEY, TipsActivity.TIP_AUTOMASSAGE);
		startActivity(intent);
	}
	
	private View resultView;
	
	private class SMSAdapter extends ArrayAdapter<SMSData> implements OnItemClickListener 
	{
		Context context;
		int viewId;
		ArrayList<SMSData> items;
		
		public SMSAdapter(Context context, int textViewResourceId, ArrayList<SMSData> objects) {
			super(context, textViewResourceId, objects);
			this.context = context;
			this.viewId = textViewResourceId;
			this.items = objects;
		}
		
		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			View view = convertView;
			
			if (view == null) {
				view = LayoutInflater.from(context).inflate(viewId, null);
			}
			
			SMSData data = items.get(position);
			
//			((TextView) view.findViewById(R.id.listSMS2Body)).setText(data.getDate());
			((TextView) view.findViewById(R.id.listSMS2Address)).setText(data.getAddress());
			((TextView) view.findViewById(R.id.listSMS2Body)).setText(data.getBody());
			
			return view;
		}
		
		@Override
		public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
			//SMSParser2 parser = new SMSParser2(items.get(arg2).getBody());
			Parser parser = null;
			try {
				parser = new Parser(items.get(arg2).getBody());
			} catch (Exception e)
			{
				Toast.makeText(superContext, "문자 분석중에 문제가 생겼습니다. 개발자에게 메일을 보내주세요.", Toast.LENGTH_SHORT).show();
				return;
			} finally
			{
				if (parser == null)
				{
					Toast.makeText(superContext, "문자 분석중에 문제가 생겼습니다. 개발자에게 메일을 보내주세요.", Toast.LENGTH_SHORT).show();
					return;
				}
			}
			parser.setDate(items.get(arg2).getDate());
			InputData data = new InputData();
			data = parser.messageParsing();
			if (data == null || !parser.getAvailable()) 
			{
				BDUtil.showDialog(SMSActivity.this, "지원불가문자", "지원하지 않는 문자 형식입니다.");
				return;
//				SMSParser parser2 = new SMSParser(items.get(arg2).getBody());
//				data = new InputData();
//				data = parser2.messageParsing();
//				
//				if (data == null || !parser2.getAvailable())
//				{
//					BDUtil.showDialog(SMSActivity.this, "지원불가문자", "지원하지 않는 문자 형식입니다.");
//					return;
//				}
			}
			
			for (int i=0; i<mArrayPaySMSData.size(); i++) {
				if (mArrayPaySMSData.get(i).getName().equalsIgnoreCase(data.getName())) {
					BDUtil.showDialog(SMSActivity.this, "중복등록", "이미 문자 자동화가 등록된 카드입니다.");
					return;
				}
			}
			
//			for (int i=0; i<mArrayPaySMSData.size(); i++) {
//				if (items.get(arg2).getAddress().equalsIgnoreCase(mArrayPaySMSData.get(i).getAddress())) {
//					BDUtil.showDialog(SMSActivity.this, "지원불가문자", "동일한 번호의 결제문자가 이미 등록되어 있습니다.");
//					return;
//				}
//			}
			
			Intent intent = new Intent(SMSActivity.this, SMSSettingActivity.class);
//			InputData data = new InputData();
			
//			SMSParser parse = new SMSParser(test);
//			data = parse.messageParsing();
//			intent.putExtra("body", items.get(arg2).getBody());
			
//			intent.putExtra("inputData", data);
			
//			String test = "신한카드정상승인이효준님 04월21일 09:02 30,000원(일시불) (주) 인터파크";	// 돈뒤
			try
			{
				Integer.parseInt((items.get(arg2).getAddress()));
			} 
			catch (Exception e)
			{
				BDUtil.showDialogOK(superContext, "오류", "문자 분석 오류 입니다.", new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface arg0, int arg1) {
						return;
					}
				});
			}
			intent.putExtra("date", items.get(arg2).getDate());
			intent.putExtra("address", items.get(arg2).getAddress());
//			intent.putExtra("body", test);
			intent.putExtra("body", items.get(arg2).getBody());
			intent.putExtra("where", "SMSActivity");
			startActivity(intent);
			if (dialog != null) {
				dialog.dismiss();
				dialog = null;
			}
		}
	}
	
	private void setSaveData() {
		SharedPreferences pref = getSharedPreferences("pref", Activity.MODE_PRIVATE);
		SharedPreferences.Editor editor = pref.edit();
		switch (((RadioGroup)findViewById(R.id.sms_radioGroup)).getCheckedRadioButtonId()) {
			case R.id.sms_radio1:
				editor.putInt("smsRadioGroup", 1);
			break;
			case R.id.sms_radio2:
				editor.putInt("smsRadioGroup", 2);
			break;
			case R.id.sms_radio3:
				editor.putInt("smsRadioGroup", 3);
			break;
			default:
			break;
		}
		editor.commit();
	}
	
	private ArrayList<SMSData> list;
	/**
	 * 다이얼로그 뛰우기 
	 */
	private class ShowDialog extends AsyncTask<Integer, Integer, Integer>{
		ProgressDialog dialog = null;
		@Override
		protected void onPreExecute() {
			super.onPreExecute();
			if (BDUtil.btnEnable)
			{
				BDUtil.btnEnable = false;
				BDUtil.asyncFlag = -1;
				
				dialog = new ProgressDialog(SMSActivity.this);
				dialog.setCancelable(false);
				dialog.setMessage("Loading...");
				dialog.show();
			}
		}
		
		@Override
		protected Integer doInBackground(Integer... arg0) {
			
			if (arg0[0] == 0) {
				if (arg0[1] == 1)
				{
					initData();
					
					return arg0[1];
				}
				else if (arg0[1] == 2)
				{
					try
					{
						sql.beginTansaction(superContext);
						
						setSaveData();
						
						sql.setTransactionSuccessful();
					} catch (Exception e)
					{
						return -1;
					} finally {
						try 
						{
							sql.endTransaction(superContext);
						} catch (Exception e) 
						{
							return -1;
						}
					}
					
					return arg0[1];
				}
				else if (arg0[1] == 3) {
					
					list = new SmsLoadInbox(superContext).loadSMS();
					
					if (list != null) {
						return arg0[1];
					} else {
						return 4;
					}
				}
				return arg0[0];
			} else 
				return arg0[0];
		}
		
		@Override
		protected void onPostExecute(Integer result) {
			super.onPostExecute(result);
			
			if (dialog != null) {
				dialog.dismiss();
			}
			
			if (result == -1)
			{
				BDUtil.btnEnable = true;
				BDUtil.asyncFlag = 0;
				
				BDUtil.showDialog(superContext, "오류", "데이터 저장에 실패했습니다.");
			}
			else if (result == 1)
			{
				
				switch (radioCheck) {
					case 1:
						((RadioGroup)findViewById(R.id.sms_radioGroup)).check(R.id.sms_radio1);
					break;
					case 2:
						((RadioGroup)findViewById(R.id.sms_radioGroup)).check(R.id.sms_radio2);
					break;
					case 3:
						((RadioGroup)findViewById(R.id.sms_radioGroup)).check(R.id.sms_radio3);
					break;
					default:
					break;
				}
				
				PaySMSAdapter adapter = new PaySMSAdapter(SMSActivity.this, R.layout.list_item_sms, mArrayPaySMSData);
				
				if (mListView.getFooterViewsCount() > 0)
				{
					mListView.removeFooterView(resultView);
				}
				
				if (mArrayPaySMSData.size() < 1)
				{
					mListView.addFooterView(resultView);
					mListView.setFooterDividersEnabled(false);
					mListView.setOnItemClickListener(null);
				}
				else
				{
					mListView.setOnItemClickListener(adapter);
				}
				
				mListView.setAdapter(adapter);
				adapter.notifyDataSetChanged();
				
				BDUtil.btnEnable = true;
				BDUtil.asyncFlag = 0;
			}
			else if (result == 2)
			{
				BDUtil.showDialogFinish(SMSActivity.this, "알림", "문자 자동화 선택 설정이 저장 되었습니다.");
			}
			else if (result == 3) 
			{
				setAddListView(list);
				BDUtil.btnEnable = true;
				BDUtil.asyncFlag = 0;
			}
			else if (result == 4)
			{
				noSMS();
				BDUtil.btnEnable = true;
				BDUtil.asyncFlag = 0;
			}
		}
	}
}
